package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Override
    public void add(Setmeal setmeal) {
        //保存套餐基本信息并返回主键
        setmealMapper.add(setmeal);
        //批量建立关系
        Integer setmealId = setmeal.getId();
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        if (CollectionUtil.isNotEmpty(checkgroupIds)) {
            setmealMapper.setSetmealAndCheckGroupBatch(setmealId, checkgroupIds);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Setmeal> getSetmeal() {
        return setmealMapper.getSetmeal();
    }

    @Override
    public Setmeal findByIdBatch(Integer id) {
        //根据套餐id查询套餐详情
        Setmeal setmeal = setmealMapper.findById(id);
        if (null != setmeal) {
            //根据套餐id查询套餐下面所有的检查组
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBySetmealId(id);
            if (CollectionUtil.isNotEmpty(checkGroups)) {
                List<Integer> ids = new ArrayList<>();
                //循环检查组查询下面的检查项
                for (CheckGroup checkGroup : checkGroups) {
                    ids.add(checkGroup.getId());
                }

                List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupIdBatch(ids);
                //手动分组
                Map<Integer, List<CheckItem>> map = new HashMap<>();

//                for (CheckGroup checkGroup : checkGroups) {//7
//                    Integer checkGroupId = checkGroup.getId();
//                    List<CheckItem> items = new ArrayList<>();
//                    for (CheckItem checkItem : checkItems) {//54
//                        if (checkGroupId.equals(checkItem.getCheckGroupId())) {
//                            items.add(checkItem);
//                        }
//                    }
//                    map.put(checkGroupId, items);
//                }
//                for (CheckItem checkItem : checkItems) {
//                    Integer checkGroupId = checkItem.getCheckGroupId();
//                    List<CheckItem> items = map.get(checkGroupId);
//                    if (null == items) {
//                        items = new ArrayList<>();
//                        map.put(checkGroupId, items);
//                    }
//                    items.add(checkItem);
//                }

                map = checkItems.stream().collect(Collectors.groupingBy(CheckItem::getCheckGroupId));

                for (CheckGroup checkGroup : checkGroups) {
                    checkGroup.setCheckItems(map.get(checkGroup.getId()));
                }
            }
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeal;
    }

    @Override
    public Setmeal findById(Integer id) {
        //根据套餐id查询套餐详情
        Setmeal setmeal = setmealMapper.findById(id);
        if (null != setmeal) {
            //根据套餐id查询套餐下面所有的检查组
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupBySetmealId(id);
            if (CollectionUtil.isNotEmpty(checkGroups)) {
                //循环检查组查询下面的检查项
                for (CheckGroup checkGroup : checkGroups) {
                    List<CheckItem> checkItems = setmealMapper.findCheckItemByCheckGroupId(checkGroup.getId());
                    checkGroup.setCheckItems(checkItems);
                }
            }
            setmeal.setCheckGroups(checkGroups);
        }
        return setmeal;
    }
}
